package com.tutsnnd.personnl_information_display_system.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tutsnnd.personnl_information_display_system.common.R;
import com.tutsnnd.personnl_information_display_system.pojo.User;
import com.tutsnnd.personnl_information_display_system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping ("/register")
    public R<String> register(@RequestBody User user){

        LambdaQueryWrapper<User> lambdaQueryWrapper= new LambdaQueryWrapper<User>()
                .eq(User::getUsername,user.getUsername());
        User one = userService.getOne(lambdaQueryWrapper);
        if (one==null){
            userService.save(user);
            return R.success("注册成功");
        }

        return R.error("用户名已存在");
    }
    @PostMapping("/login")
    public R<String> login(@RequestBody User user, HttpSession session){
        String password = user.getPassword();
        LambdaQueryWrapper<User> lambdaQueryWrapper= new LambdaQueryWrapper<User>()
                .eq(user.getUsername()!=null,User::getUsername,user.getUsername());
        User one = userService.getOne(lambdaQueryWrapper);
        if (!one.getPassword().equals(password)){
            return R.error("账号或密码不正确，请重试！");
        }
        session.setAttribute("userName",user.getUsername());
        return R.success("登陆成功");
    }
    @GetMapping("/info")
    public R<User> info(HttpSession session){
        String userName = session.getAttribute("userName").toString();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getUsername,userName);
        User user = userService.getOne(lambdaQueryWrapper);
        return R.success(user);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("userName");
        return R.success("登出成功");
    }
    @PutMapping("/image")
    public R<String> image(@RequestBody User u,HttpSession session){
        String userName = session.getAttribute("userName").toString();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getUsername,userName);
        User user = userService.getOne(lambdaQueryWrapper);
        String originalFilename = u.getImage();
//        String sufixx = originalFilename.toString().substring(originalFilename.lastIndexOf("."));
//        String filename = UUID.randomUUID().toString() + sufixx;
        user.setImage(originalFilename);
        userService.updateById(user);
        return R.success("更新成功");
    }
}
